package com.xiaoxu.intranetweb.mybatisplusSzappdb.business.statistics;

import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.StatisticWeightSheetsDayBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICWeightSheetsService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.PlantSalesVo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.StatisticWeightSheetsDayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class PeriodStatistics {
    @Autowired
    private ICWeightSheetsService icWeightSheetsService;
    //    经营日报
    public List<PlantSalesVo> statisticWeightSheetsDay(StatisticWeightSheetsDayBo statisticWeightSheetsDayBo) {
        List<StatisticWeightSheetsDayVo> statisticWeightSheetsDayVoList = icWeightSheetsService.statisticWeightSheetsDay(statisticWeightSheetsDayBo);
        // 将 StatisticWeightSheetsDayVo 对象按照厂名和产品分类进行分组，存入 salesByPlantAndClass 中
        Map<String, Map<String, List<StatisticWeightSheetsDayVo>>> salesByPlantAndClass = new HashMap<>();
        for (StatisticWeightSheetsDayVo vo : statisticWeightSheetsDayVoList) {
            String plantFullName = vo.getPlantFullName();
            String productClass = vo.getPdtClass();
            if (!salesByPlantAndClass.containsKey(plantFullName)) {
                salesByPlantAndClass.put(plantFullName, new HashMap<>());
            }
            Map<String, List<StatisticWeightSheetsDayVo>> salesByClass = salesByPlantAndClass.get(plantFullName);
            if (!salesByClass.containsKey(productClass)) {
                salesByClass.put(productClass, new ArrayList<>());
            }
            salesByClass.get(productClass).add(vo);
        }
        // 返回值封装
        List<PlantSalesVo> plantSalesVoList = new ArrayList<>();
        for (String plantFullName : salesByPlantAndClass.keySet()) {
            PlantSalesVo plantSalesVo = new PlantSalesVo();
            plantSalesVo.setPlantName(plantFullName);
            plantSalesVo.setSalesByClass(salesByPlantAndClass.get(plantFullName));
            plantSalesVoList.add(plantSalesVo);
        }
        return plantSalesVoList;
    }
    //导出经营日报excel
    public  List<StatisticWeightSheetsDayVo> weightSheetsDay(StatisticWeightSheetsDayBo statisticWeightSheetsDayBo) {
        List<StatisticWeightSheetsDayVo> statisticWeightSheetsDayVoList = icWeightSheetsService.statisticWeightSheetsDay(statisticWeightSheetsDayBo);
        if (statisticWeightSheetsDayVoList.isEmpty()) {
            return statisticWeightSheetsDayVoList;
        }
//        按照plantFullName然后pdtClass然后pdtName进行排序
        List<StatisticWeightSheetsDayVo> sortedList =
                statisticWeightSheetsDayVoList.stream()
                        .sorted(
                                Comparator.comparing(StatisticWeightSheetsDayVo::getPlantFullName)
                                        .thenComparing(StatisticWeightSheetsDayVo::getPdtClass)
                                        .thenComparing(StatisticWeightSheetsDayVo::getPdtName)
                        )
                        .collect(Collectors.toList());
        //          sortedList是按照厂名排序的一个list，每个厂名结束后，做一个厂名列为小计的数据，然后往这个list塞一个日销量小计、日销额小计、月销量小计、月销额小计、年销量小计、年销额小计值；然后在最后做一个合计值
        StatisticWeightSheetsDayVo subtotal = null; // 小计对象
        // 设置小计对象的厂名为"小计"
        StatisticWeightSheetsDayVo total = new StatisticWeightSheetsDayVo(); // 合计对象
        total.setPdtClass("合计"); // 设置合计对象的厂名为"合计"
        ListIterator<StatisticWeightSheetsDayVo> iterator = sortedList.listIterator();
        while (iterator.hasNext()) {
            StatisticWeightSheetsDayVo vo = iterator.next();
            if (subtotal == null) { // 第一次遍历
                subtotal = new StatisticWeightSheetsDayVo();
                subtotal.setPdtClass(vo.getPdtClass());
                subtotal.setPlantFullName(vo.getPlantFullName());
            } else if (!subtotal.getPdtClass().equals(vo.getPdtClass())) { // 厂名改变
                subtotal.setPdtClass("小计");
                iterator.previous(); // 将迭代器指向当前元素的前一个位置
                iterator.add(subtotal); // 在当前位置插入新元素
                iterator.next(); // 将迭代器指向当前元素的下一个位置
                subtotal = new StatisticWeightSheetsDayVo();
                subtotal.setPdtClass(vo.getPdtClass());
                subtotal.setPlantFullName(vo.getPlantFullName());
            }
// 更新小计对象的属性值
            BigDecimal daySalesVolume = subtotal.getDaySalesVolume() != null ? new BigDecimal(subtotal.getDaySalesVolume().toString()) : BigDecimal.ZERO;
            subtotal.setDaySalesVolume(daySalesVolume.add(new BigDecimal(vo.getDaySalesVolume().toString())));
            BigDecimal daySalesAmount = subtotal.getDaySalesAmount() != null ? new BigDecimal(subtotal.getDaySalesAmount().toString()) : BigDecimal.ZERO;
            subtotal.setDaySalesAmount(daySalesAmount.add(new BigDecimal(vo.getDaySalesAmount().toString())));
            BigDecimal monthSalesVolume = subtotal.getMonthSalesVolume() != null ? new BigDecimal(subtotal.getMonthSalesVolume().toString()) : BigDecimal.ZERO;
            subtotal.setMonthSalesVolume(monthSalesVolume.add(new BigDecimal(vo.getMonthSalesVolume().toString())));
            BigDecimal monthSalesAmount = subtotal.getMonthSalesAmount() != null ? new BigDecimal(subtotal.getMonthSalesAmount().toString()) : BigDecimal.ZERO;
            subtotal.setMonthSalesAmount(monthSalesAmount.add(new BigDecimal(vo.getMonthSalesAmount().toString())));
            BigDecimal yearSalesVolume = subtotal.getYearSalesVolume() != null ? new BigDecimal(subtotal.getYearSalesVolume().toString()) : BigDecimal.ZERO;
            subtotal.setYearSalesVolume(yearSalesVolume.add(new BigDecimal(vo.getYearSalesVolume().toString())));
            BigDecimal yearSalesAmount = subtotal.getYearSalesAmount() != null ? new BigDecimal(subtotal.getYearSalesAmount().toString()) : BigDecimal.ZERO;
            subtotal.setYearSalesAmount(yearSalesAmount.add(new BigDecimal(vo.getYearSalesAmount().toString())));
            BigDecimal totalDaySalesVolume = total.getDaySalesVolume() != null ? new BigDecimal(total.getDaySalesVolume().toString()) : BigDecimal.ZERO;
            total.setDaySalesVolume(totalDaySalesVolume.add(new BigDecimal(vo.getDaySalesVolume().toString())));
            BigDecimal totalDaySalesAmount = total.getDaySalesAmount() != null ? new BigDecimal(total.getDaySalesAmount().toString()) : BigDecimal.ZERO;
            total.setDaySalesAmount(totalDaySalesAmount.add(new BigDecimal(vo.getDaySalesAmount().toString())));
            BigDecimal totalMonthSalesVolume = total.getMonthSalesVolume() != null ? new BigDecimal(total.getMonthSalesVolume().toString()) : BigDecimal.ZERO;
            total.setMonthSalesVolume(totalMonthSalesVolume.add(new BigDecimal(vo.getMonthSalesVolume().toString())));
            BigDecimal totalMonthSalesAmount = total.getMonthSalesAmount() != null ? new BigDecimal(total.getMonthSalesAmount().toString()) : BigDecimal.ZERO;
            total.setMonthSalesAmount(totalMonthSalesAmount.add(new BigDecimal(vo.getMonthSalesAmount().toString())));
            BigDecimal totalYearSalesVolume = total.getYearSalesVolume() != null ? new BigDecimal(total.getYearSalesVolume().toString()) : BigDecimal.ZERO;
            total.setYearSalesVolume(totalYearSalesVolume.add(new BigDecimal(vo.getYearSalesVolume().toString())));
            BigDecimal totalYearSalesAmount = total.getYearSalesAmount() != null ? new BigDecimal(total.getYearSalesAmount().toString()) : BigDecimal.ZERO;
            total.setYearSalesAmount(totalYearSalesAmount.add(new BigDecimal(vo.getYearSalesAmount().toString())));
        }
 //将最后一个小计对象添加到列表中
        if (subtotal != null) {
            subtotal.setPdtClass("小计");
//            iterator.previous(); // 将迭代器指向当前元素的前一个位置
          //  iterator.add(subtotal); // 在当前位置插入新元素
//            iterator.next(); // 将迭代器指向当前元素的下一个位置
        }
        sortedList.add(subtotal);
// 将合计对象添加到列表中
        sortedList.add(total);
        return sortedList;
    }
}
